import hashlib

from util_store import append_url_with_subsidiary, read_url_list


def index_img(src, images_cache_path, local_path, pseudo=False):
    img_hash = hashlib.sha1(src.encode("utf-8")).hexdigest()
    if not pseudo:
        append_url_with_subsidiary(images_cache_path, img_hash, local_path, src)
        print(f"hash {img_hash} is cached with {local_path}")
    else:
        print(f"(pseudo) hash {img_hash} is cached with {local_path}")


def init_img_index(images_cache_path="cache"):
    img_cache_dict = read_url_list(images_cache_path)
    return img_cache_dict


def get_index_img(img_cache_dict, src):
    img_hash = hashlib.sha1(src.encode("utf-8")).hexdigest()
    if img_hash in img_cache_dict.keys():
        return img_cache_dict[img_hash]
    else:
        return None